<template>
  <div class="box">
    <!-- 头部 -->
    <div class="headBox">
      <van-icon name="arrow-left" class="icon_bo" @click="fanhui" />
      <h5 class="chattext">当前问诊</h5>
    </div>
    <!-- 主体部分 -->
    <div class="main">
      <div
        v-if="
          isInquiryActive &&
          listdata.getdocterResuly &&
          Object.keys(listdata.getdocterResuly).length > 0
        "
      >
        <div>
          <div class="imgbox">
            <!-- <img :src="listdata.getdocterResuly.imagePic" alt="Doctor Image" /> -->
             <Image style="width: 273px;height: 330px;" :src="listdata.getdocterResuly.imagePic" alt="Doctor Image" ></Image>
            <div class="contion_box">
              <h3 class="contion_listname">{{ listdata.getdocterResuly.doctorName }}</h3>
              <p class="contion_listname">{{ listdata.getdocterResuly.jobTitle }}</p>
              <p class="contion_listname">{{ listdata.getdocterResuly.department }}</p>
              <p class="contion_listname">问诊时间:{{ listdata.getdocterResuly.inquiryTime }}</p>
            </div>
          </div>
        </div>
        <!-- <div class="contion_box"> -->
        <!-- <h3 class="contion_listname">{{ listdata.getdocterResuly.doctorName }}</h3>
        <p class="contion_listname">{{ listdata.getdocterResuly.jobTitle }}</p> -->
        <!-- </div> -->
        <div>
          <van-button type="default" class="buttonaa">继续问诊</van-button>
          <van-button type="primary" class="buttonaa" @click="end()">结束问诊</van-button>
        </div>
      </div>
      <!-- 暂无问诊记录 -->
      <div v-else>
        <van-empty description="暂无问诊">
          <!-- 返回按钮 -->
          <van-button
            round
            style="width: 160px; background-color: blue; color: #fff"
            class="bottom-button"
            @click="fanhuiaa()"
            >返回</van-button
          >
        </van-empty>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import router from '@/router'
import { usecurrentStore } from '../../stores/Consultation'
import { onMounted, ref } from 'vue'
import { endpingData } from '../../api/Consultation'
import Image from '@/components/image/index.vue'

const listdata: any = usecurrentStore()
const isInquiryActive = ref(true)

const fanhui = () => {
  router.push('/my')
}
const fanhuiaa = () => {
  router.push('/my')
}
onMounted(async () => {
  const userId = parseInt(JSON.parse(localStorage.getItem('userId') || '0'), 10)
  const sessionId = JSON.parse(localStorage.getItem('sessionId') || '""')
  // 调用actions中的方法获取数据
  await listdata.handdocterData(userId, sessionId)
  console.log(listdata.getdocterResuly, 'rjfhui')
})

// 结束问诊
const end = async () => {
  try {
    const recordId = listdata.getdocterResuly.recordId // 假设recordId存储在getdocterResuly对象中
    console.log(recordId, 'bailing')

    const userId = JSON.parse(localStorage.getItem('userId') || '')
    const sessionId = JSON.parse(localStorage.getItem('sessionId') || '""')
    // 调用endpingData函数结束问诊
    const response = await endpingData(userId, sessionId, recordId)
    console.log(response, 'response')

    if (response && response.status === 200) {
      console.log('问诊结束成功')
      isInquiryActive.value = false // 更新状态，表示问诊已结束
    } else {
      console.error('问诊结束失败', response)
    }
  } catch (error) {
    console.error('结束问诊时发生错误', error)
  }
}
</script>
<style scoped lang="scss">
.box {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
}
// 头部
.headBox {
  width: 100%;
  height: 40px;
  background-color: white;
  border-bottom: 1px solid #ccc;
  .icon_bo {
    float: left;
    line-height: 40px;
    font-size: 20px;
  }
  .chattext {
    text-align: center;
    line-height: 40px;
    font-size: 15px;
  }
}
// 主体
.main {
  flex: 1;
}
.imgbox {
  width: 80%;
  //   height: 1rem;
  margin-left: 1.3rem;
}
.contion_box {
  width: 90%;
  height: 4rem;
  border: 1px solid gray;
  margin-left: 0.5rem;
  margin-top: 0.1rem;
  .contion_listname {
    text-align: center;
    margin-top: 0.4rem;
  }
}
.buttonaa {
  margin-left: 1.8rem;
  margin-top: 20px;
}
</style>
